<?php defined('SYSPATH') or die('No direct script access.');

/**
 * Created by PhpStorm.
 * Author: lh
 * Time: 2020/07/14
 * Desc: 私人定制
 */
class Controller_Pc_Api_V2_Customize extends Controller_Pc_Api_v2_Base
{

    // 数据请求结果
    private $result;

    /**
     * 参数验证对象
     * @var Request_Api_V2_Customize
     */
    private $validation;

    /**
     * 服务
     * @var Service_Api_Customize
     */
    private $service;

    public function before()
    {
        parent::before();

        // 验证
        $this->validation = Request_Api_V2_Factory::create('customize', $this->request_params);

        // 服务
        $this->service = new Service_Api_Customize();

        // 返回数据
        $this->result = array('status' => true, 'msg' => '操作成功', 'data' => array());
    }

    public function after()
    {
        $this->send_datagrams($this->client_info['id'], $this->result, $this->client_info['secret_key']);
    }

    // 计划
    public function action_plan()
    {
        try {
            $this->validation->plan()->check();

            $this->result['data'] = $this->service->plan($this->request_params);
        } catch (Exception $e) {
            $this->result['status'] = false;
            $this->result['msg']    = $e->getMessage();
        }
    }

    public function action_plan_where() {
        try {
            $this->validation->plan_where()->check();
            $this->result['data'] = $this->service->plan_where();
        } catch (Exception $e) {
            $this->result['status'] = false;
            $this->result['msg']    = $e->getMessage();
        }
    }

    public function action_plan_more() {
        try {
            $this->validation->plan_more()->check();
            $this->result['data'] = $this->service->plan_more($this->request_params);
        } catch (Exception $e) {
            $this->result['status'] = false;
            $this->result['msg']    = $e->getMessage();
        }
    }

    // 计划详情
    public function action_plan_show()
    {
        try {
            $this->validation->plan_show()->check();

            $this->result['data'] = $this->service->plan_show($this->request_params['id']);
        } catch (Exception $e) {
            $this->result['status'] = false;
            $this->result['msg']    = $e->getMessage();
        }
    }

    // 扩展字段
    public function action_extend_fields()
    {
        try {
            $this->result['data'] = $this->service->extend_fields();
        } catch (Exception $e) {
            $this->result['status'] = false;
            $this->result['msg']    = $e->getMessage();
        }
    }

    // 预订下单
    public function action_add_order()
    {
        try {
            $this->validation->add_order()->check();

            $this->result['data'] = $this->service->add_order($this->request_params);
        } catch (Exception $e) {
            $this->result['status'] = false;
            $this->result['msg']    = $e->getMessage();
        }
    }

    // 预订详情
    public function action_show()
    {
        try {
            $this->validation->show()->check();

            $this->result['data'] = $this->service->show($this->request_params['product_id']);
        } catch (Exception $e) {
            $this->result['status'] = false;
            $this->result['msg']    = $e->getMessage();
        }
    }

    public function action_get_max_use_jifen()
    {
        try {
            $this->validation->show()->check();

            $this->result['data']['deduct'] = $this->service->get_max_use_jifen($this->request_params);
        } catch (Exception $e) {
            $this->result['status'] = false;
            $this->result['msg']    = $e->getMessage();
        }
    }

    // 完善订单信息
    public function action_improve_order_info()
    {
        $db = Database::instance();
        $db->begin();
        try {
            $this->validation->improve_order_info()->check();


            $this->result['data'] = $this->service->improve_order_info($this->request_params);

            $db->commit();
        } catch (Exception $e) {
            $this->result['status'] = false;
            $this->result['msg']    = $e->getMessage();

            $db->rollback();
        }
    }

}
